﻿using CefSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MainFrame.Plugs
{
    public class MyResponseFilter : IResponseFilter
    {
        public List<byte> DataAll = new List<byte>();

        public void Dispose()
        {
        }

        public FilterStatus Filter(Stream dataIn, out long dataInRead, Stream dataOut, out long dataOutWritten)
        {
            try
            {
                if (dataIn == null || dataIn.Length == 0)
                {
                    dataInRead = 0;
                    dataOutWritten = 0;
                    return FilterStatus.Done;
                }
                using(var ms = new MemoryStream())
                {
                    dataIn.CopyTo(ms);
                    var buffer = ms.ToArray();
                    DataAll.AddRange(buffer);
                    dataOut.Write(buffer, 0, buffer.Length);
                }

                dataInRead = dataIn.Length;
                dataOutWritten = dataIn.Length;

                return FilterStatus.NeedMoreData;
            }
            catch (Exception ex)
            {
                dataInRead = dataIn.Length;
                dataOutWritten = dataIn.Length;

                return FilterStatus.Done;
            }
        }

        public bool InitFilter()
        {
            return true;
        }
    }
}
